<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 角色信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
          &nbsp;&nbsp;<el-button type="primary" size="small" @click="addVisible = true">添加角色</el-button>&nbsp;
          <el-button type="primary" size="small" @click="dels">批量删除</el-button>&nbsp;
      <div class="handle-box">
        <el-input v-model="title" placeholder="标题名称" class="handle-input mr10"></el-input>
        <el-select v-model="status" placeholder="角色状态">
            <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
         height="500px"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40" align="center"></el-table-column>
        <el-table-column property="title" label="标题" align="center"></el-table-column>
        <el-table-column width="55" label="状态" align="status" :formatter="fgf"></el-table-column>
        <el-table-column property="rules" label="所有权限" align="center"></el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" @click="open(scope.$index, scope.row)">删除</el-button>
      <!--   <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="Del(scope.$index, scope.row)"
              >删除</el-button
            >    -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[1, 2, 3, 5]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog v-bind="$attrs" title="编辑" v-model="editVisible">
  <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="150px">
    <el-row :gutter="16">
      <el-col :span="12">
        <el-form-item label="角色标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="员工状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择员工状态" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label"
              :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
        <el-col :span="12">
        <el-form-item label="所有权限" prop="rules">
          <el-input v-model="form.rules" placeholder="请输入权限" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEdit">确 定</el-button>
        </span>
      </template>
</el-dialog>
  </div>
</template>

<script>
import axios from "axios";
// ,updateEmp,deleEmp,fanEmp 
import { AuthShow,updateAuth,getAuth,deleAuth,delAuth} from "../../api/system.js";
export default {
  name: "basetable",
  data() {
    return {
        title:'',
        status: 1,
        pageIndex: 1,
        pageSize: 3,
      options: [
        {
          value: "1",
          label: "存在",
        },
        {
          value: "0",
          label: "不存在",
        },
      ],
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {
        title:'',
        status:'',
        rules:''
      },
      data() {
        return {
          options: [],
          value: "",
        };
      },
      idx: -1,
      id: -1,
      statusOptions: [{
        "label": "存在",
        "value": 1
      }, {
        "label": "不存在",
        "value": 0
      }],
    };
  },
  created() {
    this.Show();
  },
  methods: {
    fgf(row) {
      if (row.status == "1") {
        return <span style="color:green">存在</span>;
      } else {
        return <span style="color:red">不存在</span>;
      }
    },
    Show() {
      AuthShow({
          PageSize: this.pageSize,
          PageIndex: this.pageIndex,
          status: this.status,
          title: this.title,
        })
        .then((res) => {
        console.log(res.data);
        this.tableData = res.data.pagedData;
        this.pageTotal = res.data.totalCount;
      });
    },
   
    // 触发搜索按钮
    handleSearch() {
      this.Show();
    },
    //状态显示
    statusShow(value) {
      if (value == 1) {
        this.status = 1;
        this.Show();
      } else {
        this.status = 0;
        this.Show();
      }
    },
     // 删除操作
     handleDelete(index) {
       // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
         type: "warning",
       })
         .then(() => {
           this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
     },

    //删除操作
     open(index,row) {
      alert(row.id);
      deleAuth({
        objId:row.id
      }).then((res) => {
        console.log(res);
        if (res.code == 1) {
          alert("删除成功");
          this.Show();
        } else {
          alert("删除失败");
          this.Show();
        }
      });
    },
      dels(){
            for(var i=0;i<this.multipleSelection.length;i++){
                this.delLists.push(this.multipleSelection[i].id);
            }
            alert(this.delLists);
            if(confirm("请确认是否进行删除!!!")){
                delAuth({
                objId: 0+this.delLists,
                }).then((res) => {
                console.log(res);
                if (res.code == 1) {
                    alert("批量删除成功");
                    this.Show();
                } else {
                    alert("批量删除失败");
                    this.Show();
                }
            });
        }
        this.delLists=[];
      },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      getAuth({
        id:row.id
      })
      .then((res) => {
        console.log(res);
        this.form = res.data;
      });
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      updateAuth(this.form)
      .then((res) => {
        if (res.code > 0) {
          alert("修改成功");
          this.Show();
        } else {
          alert("修改失败");
        }
      });
      this.editVisible = false;
    },
    // 分页大小
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.Show();
    },
    //分页页码
    handleCurrentChange(pageIndex) {
      this.pageIndex = pageIndex;
      this.Show();
    },
       Del(index, row) {
      console.log(index, row.id);
      if (confirm("确认删除吗")) {
       axios({
          method: "DELETE",
          url: "https://localhost:5001/api/AuthGroup/Delete?Id=" + row.id,
        }).then((res) => {
          if (res.data.code == 204) {
            alert("删除成功");
            this.Show();
          } else {
            alert("删除失败");
            this.Show();
          }
        });
      }
       }
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
